﻿using System;
using System.Collections.Generic;
using System.Linq;

using Haushaltsbuch.DataAcces;
using Haushaltsbuch.DataAcces.Entities;
using Haushaltsbuch.Domain.Authentication;
using Haushaltsbuch.Helpers;

namespace Haushaltsbuch.Tests
{
    public static class TestHelpers
    {
        public static void LoginAsAdmin()
        {
            HaushaltsbuchPrincipal.Login("Admin", "test");
        }
        public static void LoginAsReader()
        {
            HaushaltsbuchPrincipal.Login("Reader", "test");
        }
        public static void LoginAsContributor()
        {
            HaushaltsbuchPrincipal.Login("Contributor", "test");
        }


        public static void InitDbForTest()
        {
            using (var ctx = new HaushaltsbuchContext())
            {
                ctx.Database.ExecuteSqlCommand("DELETE FROM Users");

                var reader = ctx.Roles.Single(r => r.Name == "Reader");
                var contributor = ctx.Roles.Single(r => r.Name == "Contributor");
                var admin = ctx.Roles.Single(r => r.Name == "Admin");

                foreach (var user in new[]
                    {
                        new User
                            {
                                Lastname = "Blub",
                                Firstname = "Reader",
                                Roles = new HashSet<Role> {reader},
                                Passwort = PasswordHelper.GetHash("test"),
                                Username = "Reader"
                            },
                        new User
                            {
                                Lastname = "Blub",
                                Firstname = "Contributor",
                                Roles = new HashSet<Role> {contributor},
                                Passwort = PasswordHelper.GetHash("test"),
                                Username = "Contributor"
                            },
                        new User
                            {
                                Lastname = "Blub",
                                Firstname = "Admin",
                                Roles = new HashSet<Role> {admin, reader},
                                Passwort = PasswordHelper.GetHash("test"),
                                Username = "Admin"
                            },
                    })
                    ctx.Users.Add(user);

                ctx.SaveChanges();
            }
        }
 
    }
}